// pages/my/balanceWithdraw/index.js
const api = require('../../../fetch/api')
const app = getApp()
const {
  baseURL, ip
} = require('../../../fetch/http')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    ip: ip,
    account: '',
    show: false,
    money: '',
    qr: '', // 二维码
    realname: '', // 姓名
    type: 1, // 0 微信 1 支付宝(默认)
  },
  inputName(e) {
    this.setData({
      realname: e.detail.value
    })
  },
  uploadImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res)=>{
        const filePath = res.tempFilePaths[0]
        api.uploadImage(filePath).then(res=>{
          if(res.code==200) {
            this.setData({
              qr: res.data[0].src
            })
          }
        })
      }
    })
  },
  switchType(e) {
    const {
      index
    } = app.dataSet(e)
    this.setData({
      type: index
    })
    this.close()
    // 微信
    if(index==0){
      this.setData({
        qr: this.data.wxQr
      })
    }else {
      this.setData({
        qr: this.data.alipayQr
      })
    }
    
  },
  inputAlipay(e) {
    this.setData({
      account: e.detail.value
    })
  },
  inputMoney(e) {
    this.setData({
      money: e.detail.value
    })
  },
  show() {
    this.setData({
      show: true
    })
  },
  close() {
    this.setData({
      show: false
    })
  },
  async withdraw() {
    let {
      type,
      money,
      account,
      commission,
      qr,
      realname
    } = this.data
    if (money > commission) {
      app.toast('余额不足')
      return
    } else if (!money) {
      app.toast('请输入提现金额')
      return
    } else if(!realname) {
      app.toast('请输入账号姓名')
      return
    }else if(!qr) {
      app.toast('请上传收款码')
      return
    }

    let res = await api.withdraw({
      fund_type: 1, // 红包
      money,
      type,
      account,
      realname,
      qr
    })
    if (res.code == 200) {
      this.setData({
        money: ''
      })
      app.toast('申请成功', 'success', () => {
        const remaining = (commission*100 - money*100)/100
        this.setData({
          commission: remaining.toFixed(2)
        })
      })
    }
  },
  async redPacket() {
    let res = await api.redPacket()
    // console.log(res);
    let {
      commission,
      withdrawCommissionFeeRatio,
      withdrawMinMoney,
      alipayQr,wxQr,realname
    } = res.data
    this.setData({
      commission,
      withdrawCommissionFeeRatio,
      withdrawMinMoney,
      alipayQr,wxQr,
      qr: alipayQr,
      realname
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.redPacket()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})